import request from '@/utils/request'

export interface ArticleParam {
  current?: number
  pageSize?: number
  setm?: string
  keyWord?: string
  sorter?: string
}

export interface ServiceResponse<T> {
  code: number
  data: T
  message: string
}
export interface ArticlesResponse {
  current: number
  pageSize: number
  pageTotal: number
  total: number
  rows: ArticleRowItem[]
}

// 收藏、点赞操作参数
interface OptParam {
  // 面经id
  id: string
  // 操作类型: 1点赞 2收藏
  optType: number
}
// 文章列表项
export interface ArticleRowItem {
  id: string
  avatar: string
  collectFlag: number
  content: string
  createdAt: string
  creator: string
  likeCount: number
  likeFlag: number
  stem: string
  views: number
}
export const getArticlesAPI = (
  params: ArticleParam,
): Promise<ServiceResponse<ArticlesResponse>> => {
  // const token = getStorageToken()
  return request.get('/h5/interview/query', {
    params: {
      current: params.current || 1,
      pageSize: params.pageSize || 10,
      sorter: params.sorter,
    },
  })
}

export const getArticleDetailAPI = (id: string): Promise<ServiceResponse<ArticleRowItem>> => {
  return request.get('/h5/interview/show', {
    params: {
      id: id,
    },
  })
}

// 收藏、点赞题目/取消收藏、点赞题目接口
export const articleOptAPI = (
  data: OptParam,
): Promise<ServiceResponse<null>> => {
  return request.post('/h5/interview/opt', data)
}

// 获取收藏、点赞列表接口
export const getOptListAPI = (
  params: {
    optType: number // 操作类型: 1点赞 2收藏
    pageSize?: number
    page?: number
  }
): Promise<ServiceResponse<ArticlesResponse>> => {
  return request.get('/h5/interview/opt/list', {
    params: {
      optType: params.optType,
      pageSize: params.pageSize || 10,
      page: params.page || 1
    }
  })
}

